/* eslint-disable react/sort-comp */
import { messages } from "share/common";
import React, { Component } from "react";
import { Form, Button, Spin, Radio, Checkbox, Input, Row, Icon, Col, Affix } from "antd";
import AirCardForm from "../components/air-card-form";
import AirLineServer from "../airline-server";
import { message } from "antd/lib/index";
import "styles/supplier-management/airline-config/platform-common.scss";
import Condition from "components/condition";

const { If } = Condition;

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const CheckboxGroup = Checkbox.Group;

@Form.create()
class SetInternational extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      emails: [""], // 邮箱
      bodyData: {},
      initData: {}, // 通过接口拿到初始值
      flightRfpVendorSignsDefault: [],
      supplier: [],
    };
  }

  componentWillMount() {
    this.initData();
  }

  initData = () => {
    AirLineServer(this)
      .getAirInternationalData(this.props.companyOID)
      .then((res) => {
        if (res.data.body) {
          const defaultSupplier = [];
          this.setState({
            initData: res.data.body,
            supplier: res.data.body.flightRfpVendorSigns || [],
            flightRfpVendorSignsDefault: defaultSupplier,
            bodyData: res.data.body || {},
            emails: res.data.body.internationalEmail ? res.data.body.internationalEmail.split(",") : [""],
            rfpVendorNames: res.data.body.rfpVendorNames || messages("supplier-1.key831") /*暂无启用机票供应商*/,
          });
        } else if (res.data.message) {
          message.error(res.data.message);
        }
      });
  };

  onSaveSetting = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const { initData, supplier } = this.state;
        let international = {};
        const emails = [];
        for (const key in values) {
          if (key.includes("emails")) {
            emails.push(values[key]);
          }
        }
        if (values.isEnabledInternationalEmail) {
          international.internationalEmail = emails.join(",");
        }

        international.isEnabledInternationalEmail = values.isEnabledInternationalEmail;
        international.firstEmailSendWay = values.firstEmailSendWay;
        international.sendCertificateWay = values.sendCertificateWay;
        international.internationalRfpWay = values.internationalRfpWay;
        international.isEnabledCcBooker = values.isEnabledCcBooker;
        international = Object.assign(initData, international);
        international.companyOid = this.props.companyOID;
        AirLineServer(this)
          .saveAirInternationalData(international)
          .then((res) => {
            if (res.data.success) {
              this.initData();
              message.success(messages("supplier-1.key355") /*保存成功*/);
            } else {
              message.error(res.data.message);
            }
          });
      }
    });
  };

  onAddEmail = (index) => {
    const { emails } = this.state;
    if (index === 0) {
      emails.push("");
    } else {
      emails.splice(index, 1);
    }
    this.setState({ emails });
  };

  render() {
    const { loading, emails, bodyData, rfpVendorNames } = this.state;
    const { getFieldDecorator, getFieldValue } = this.props.form;
    const spanNum2 = 12;
    const formLayout = { width: "90%", display: "inline-block" };
    return (
      <div className="airline-config-container">
        <Spin spinning={loading} className="spin">
          <Form>
            <h2>{messages("supplier-1.key362") /*国际行程邮件通知*/}</h2>
            <a className="a-line-bottom">
              {
                messages(
                  "supplier-1.key363"
                ) /*可设置指定邮箱，当国际机票行程审批通过后，可及时邮件通知供应商，及时的为您的员工提供服务*/
              }
            </a>
            <FormItem>
              {getFieldDecorator("isEnabledInternationalEmail", {
                initialValue: bodyData.isEnabledInternationalEmail || false,
                valuePropName: "checked",
              })(<Checkbox>{messages("supplier-1.key35") /*启用temp*/}</Checkbox>)}
            </FormItem>
            <If value={getFieldValue("isEnabledInternationalEmail")}>
              <Row style={{ paddingBottom: 30 }} className="platform-common-class">
                {emails.map((item, index) => {
                  return (
                    <Col span={spanNum2} style={{ marginTop: index > 1 ? 12 : 0 }}>
                      <FormItem label={`${messages("supplier-1.key354") /*客服邮箱*/}${index + 1}`} style={formLayout}>
                        {getFieldDecorator(`emails${index}`, {
                          initialValue: item || "",
                        })(<Input />)}
                      </FormItem>
                      <Icon
                        onClick={() => this.onAddEmail(index)}
                        type={index != 0 ? "minus-circle" : "plus-circle"}
                        className="plus-icon"
                      />
                    </Col>
                  );
                })}
              </Row>
            </If>
            <br />
            <br />
            <h2>{messages("supplier-1.key364") /*邮件通知（第一次询价）时间点*/}</h2>
            <a className="a-line-bottom" style={{ paddingBottom: 0 }} />
            <FormItem>
              {getFieldDecorator("firstEmailSendWay", {
                initialValue: bodyData.firstEmailSendWay || "auto",
              })(
                <RadioGroup>
                  <Radio value="auto">{messages("supplier-1.key365") /*审批通过后自动通知*/}</Radio>
                  <Radio value="manually">{messages("supplier-1.key366") /*员工主动触发*/}</Radio>
                </RadioGroup>
              )}
            </FormItem>
            <br />
            <br />
            <h2>{messages("supplier-1.key367") /*邮件询价时是否带乘机人证件信息*/}</h2>
            <a className="a-line-bottom" style={{ paddingBottom: 0 }} />
            <FormItem>
              {getFieldDecorator("sendCertificateWay", {
                initialValue: bodyData.sendCertificateWay === 0 ? 0 : 1,
              })(
                <RadioGroup>
                  <Radio value={0}>{messages("supplier-1.key368") /*不带证件进行查询*/}</Radio>
                  <Radio value={1}>{messages("supplier-1.key369") /*带证件信息查询*/}</Radio>
                </RadioGroup>
              )}
            </FormItem>
            <br />
            <br />
            <h2>{messages("supplier-1.key370") /*邮件询价时是否抄送预订人*/}</h2>
            <a className="a-line-bottom" style={{ paddingBottom: 0 }} />
            <FormItem>
              {getFieldDecorator("isEnabledCcBooker", {
                initialValue: bodyData.isEnabledCcBooker || false,
              })(
                <RadioGroup>
                  <Radio value>{messages("supplier-1.key371") /*抄送*/}</Radio>
                  <Radio value={false}>{messages("supplier-1.key372") /*不抄送*/}</Radio>
                </RadioGroup>
              )}
            </FormItem>
            <br />
            <br />
            <h2>{messages("supplier-1.key373") /*国际行程询价方式*/}</h2>
            <a className="a-line-bottom" style={{ paddingBottom: 0 }} />
            <FormItem>
              {getFieldDecorator("internationalRfpWay", {
                initialValue: bodyData.internationalRfpWay || "single",
              })(
                <RadioGroup>
                  <Radio value="single">{messages("supplier-1.key374") /*按行程询价*/}</Radio>
                  {/*<Radio value='merge'>{messages('supplier-1.key375')/*按整个申请单多程询价*/}
                </RadioGroup>
              )}
            </FormItem>
            <br />
            <br />
            <h2>{messages("supplier-1.key376") /*启用的服务商*/}</h2>
            <a className="a-line-bottom" style={{ paddingBottom: 0 }} />
            <div>{rfpVendorNames}</div>
          </Form>
          <br />
          <br />
          <br />
          <br />
          <br />
          <br />
        </Spin>
        <Affix offsetBottom={0} className="setting-fotter-class">
          <Button style={{ marginLeft: 20 }} type="primary" onClick={this.onSaveSetting}>
            {messages("supplier-1.key356") /* 保存配置*/}
          </Button>
        </Affix>
      </div>
    );
  }
}

export default SetInternational;
